package com.bcbim.api_web.role.controller;

import com.bcbim.authentication_interface.role.dao.RoleDAO;
import com.bcbim.authentication_interface.role.dto.RoleDTO;
import com.bcbim.authentication_interface.role.service.RoleService;
import com.bcbim.authentication_interface.role.vo.RoleListVO;
import com.bcbim.authentication_interface.role.vo.RoleVO;
import com.bcbim.authentication_interface.util.AuthUtil;
import com.bcbim.common.response.vo.ReturnMsg;
import com.bcbim.common.response.vo.ReturnPageMsg;
import com.bcbim.common.sys.Code;
import com.bcbim.common.sys.Constant;
import com.bcbim.common.sys.ParamException;
import com.bcbim.common.util.GuidUtil;
import com.bcbim.common.util.ParamCheck;
import com.google.common.collect.Lists;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description:
 * @Author XueHao
 * @Date 2021/8/16
 * @Version V1.0
 **/
@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference(protocol = "dubbo", group = "role", version = "1.0.0")
    private RoleService roleServiceImpl;

    /**
     * @return ReturnPageMsg
     * @Description 查看角色列表
     * @Author XueHao
     * @Date 2021/8/16
     */
    @PostMapping("/listRole")
    public ReturnPageMsg roleList(RoleDTO roleDTO) {
        try {
            ParamCheck.pageNumCheck(roleDTO.getPageNum(), roleDTO.getPageSize());
        } catch (ParamException e) {
            return ReturnPageMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        //创建空集合
        List<RoleListVO> list = Lists.newArrayList();
        //判断查询的条数
        int total = roleServiceImpl.getRoleListCount(roleDTO);
        if (total > 0) {
            //计算起始页
            roleDTO.setStart((roleDTO.getPageNum() - 1) * roleDTO.getPageSize());
            list = roleServiceImpl.listRole(roleDTO);
        }
        return ReturnPageMsg.consSuccess(total, list);
    }

    /**
     * @return ReturnMsg
     * @Description 删除角色
     * @Author XueHao
     * @Date 2021/8/16
     */
    @PostMapping("/removeRole")
    public ReturnMsg removeRole(String id) {
        //参数校验
        try {
            //id校验
            ParamCheck.idCheck(id);
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        int result = roleServiceImpl.removeRole(id);
        return result == 1 ? ReturnMsg.consSuccess(Constant.DEL_SUCCESS) : ReturnMsg.consFail(Constant.DEL_FAIL);
    }

    /**
     * @return ReturnMsg
     * @Description 新增角色
     * @Author XueHao
     * @Date 2021/8/16
     */
    @PostMapping("/insertRole")
    public ReturnMsg insertRole(RoleDAO roleDAO) {
        try {
            //参数校验
            ParamCheck.stringNotEmptyRegexpCheck(roleDAO.getEnable(), "^[0|1]$");
            ParamCheck.stringNotEmpty(roleDAO.getRoleName(), 20);
            ParamCheck.stringNotEmptyRegexpCheck(roleDAO.getQueueNumber(), "^([1-9][0-9]*)$");
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        //生成人员ID
        roleDAO.setId(GuidUtil.get());
        //获取登录用户ID
        String userId = AuthUtil.getUserIdDecrypt();
        //设置创建人ID
        roleDAO.setCreateBy(userId);
        //设置创建时间
        roleDAO.setCreateTime(LocalDateTime.now());
        //修改删除标记
        roleDAO.setDel(Constant.NOT_DEL);
        //执行情况
        int result = roleServiceImpl.insertRole(roleDAO);
        return result == 1 ? ReturnMsg.consSuccess(Constant.SAVE_SUCCESS) : ReturnMsg.consFail(Constant.SAVE_FAIL);
    }

    /**
     * @return ReturnMsg
     * @Description 更改角色
     * @Author XueHao
     * @Date 2021/8/17
     */
    @PostMapping("/updateRole")
    public ReturnMsg updateRole(RoleDAO roleDAO) {
        try {
            //参数校验
            ParamCheck.stringNotEmptyRegexpCheck(roleDAO.getEnable(), "^[0|1]$");
            ParamCheck.idCheck(roleDAO.getId());
            ParamCheck.stringNotEmpty(roleDAO.getRoleName(), 20);
            ParamCheck.stringNotEmptyRegexpCheck(roleDAO.getQueueNumber(), "^([1-9][0-9]*)$");
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        roleDAO.setUpdateBy(AuthUtil.getUserIdDecrypt());
        //调用服务
        int count = roleServiceImpl.updateRole(roleDAO);
        return count == 1 ? ReturnMsg.consSuccess("修改成功") : ReturnMsg.consFail("修改失败");
    }

    /**
     * @return null
     * @Description 状态下拉
     * @Author XueHao
     * @Date 2021/8/20
     */
    @PostMapping("/selectEnable")
    public ReturnMsg selectEnable() {
        //调用服务
        List<Code> selectEnable = roleServiceImpl.selectEnable();
        return ReturnMsg.consSuccess(selectEnable);
    }

    /**
     * @return ReturnMsg
     * @Description 查询角色详情
     * @Author XueHao
     * @Date 2021/8/23
     */
    @PostMapping("/getRole")
    public ReturnMsg getRole(String id) {
        // 参数校验
        try {
            //id校验
            ParamCheck.idCheck(id);
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        // 查询数据库
        RoleVO roleVO = roleServiceImpl.getRoleVO(id);
        return ReturnMsg.consSuccess(roleVO);
    }
}